package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.HotProjectBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.NearByShopBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ISearchView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/6/8.
 */

public class SearchPresenter extends BasePresenter<ISearchView> {

    public SearchPresenter(ISearchView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void searchShop(String keyword, double lat, double lng) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).search_shop(keyword, "1", lat, lng)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<NearByShopBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<NearByShopBean>> response) {
                        iView.setShopList(response.getData());
                    }
                }));
    }

    public void searchProduct(String keyword) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).search_product(keyword, "2")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<ShopListBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<ShopListBean>> response) {
                        iView.setProductList(response.getData());
                    }
                }));
    }

    public void searchProject(String keyword) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).search_project(keyword, "3")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<HotProjectBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<HotProjectBean>> response) {
                        iView.setProjectList(response.getData());
                    }
                }));
    }
}
